package com.example.team.dao.Imp;

import com.example.team.dao.ISafetyEquipmentPurchaseDateDao;
import com.example.team.entity.SafetyEquipmentPurchaseDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 *根据接口操作数据库表的接口规范，对其进行实现
 * 编写者：蓝江富
 * 时间：2023年10月31日18:33:55
 */
@Repository
public class ISafetyEquipmentPurchaseDateDaoImpl implements ISafetyEquipmentPurchaseDateDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int insertSafetyEquipmentPurchaseDate(SafetyEquipmentPurchaseDate safetyEquipmentPurchaseDate) {
        return jdbcTemplate.update(
                "insert into safety_equipment_purchase_date(" +
                        "PurchaseDateId," +
                        "ItemName," +
                        "quantity," +
                        "examiner," +
                        "examineDate " +
                        ") " +
                        "values(?,?,?,?,?)"
                ,safetyEquipmentPurchaseDate.getPurchaseDateId()
                ,safetyEquipmentPurchaseDate.getItemName()
                ,safetyEquipmentPurchaseDate.getQuantity()
                ,safetyEquipmentPurchaseDate.getExaminer()
                ,safetyEquipmentPurchaseDate.getExamineDate()
        );
    }

    @Override
    public int deleteSafetyEquipmentPurchaseDate(SafetyEquipmentPurchaseDate safetyEquipmentPurchaseDate) {
        return jdbcTemplate.update(
                "delete from safety_equipment_purchase_date " +
                "where PurchaseDateId=?",safetyEquipmentPurchaseDate.getPurchaseDateId());
    }

    @Override
    public int updateSafetyEquipmentPurchaseDate(SafetyEquipmentPurchaseDate safetyEquipmentPurchaseDate) {
        //如果更新的字段为空，则保留旧数据
        SafetyEquipmentPurchaseDate safetyEquipmentPurchaseDate1=getSafetyEquipmentPurchaseDateById(safetyEquipmentPurchaseDate);
        if(safetyEquipmentPurchaseDate.getItemName()==null)
            safetyEquipmentPurchaseDate.setItemName(safetyEquipmentPurchaseDate1.getItemName());
        if(safetyEquipmentPurchaseDate.getQuantity()==null)
            safetyEquipmentPurchaseDate.setQuantity(safetyEquipmentPurchaseDate1.getQuantity());
        if(safetyEquipmentPurchaseDate.getExaminer()==null)
            safetyEquipmentPurchaseDate.setExaminer(safetyEquipmentPurchaseDate1.getExaminer());
        if(safetyEquipmentPurchaseDate.getExamineDate()==null)
            safetyEquipmentPurchaseDate.setExamineDate(safetyEquipmentPurchaseDate1.getExamineDate());
        return jdbcTemplate.update("update safety_equipment_purchase_date set " +
                        "ItemName=?," +
                        "quantity=?," +
                        "examiner=?," +
                        "examineDate=? " +
                        "where PurchaseDateId=?"
                ,safetyEquipmentPurchaseDate.getItemName()
                ,safetyEquipmentPurchaseDate.getQuantity()
                ,safetyEquipmentPurchaseDate.getExaminer()
                ,safetyEquipmentPurchaseDate.getExamineDate()
                ,safetyEquipmentPurchaseDate.getPurchaseDateId()
        );
    }

    @Override
    public List<SafetyEquipmentPurchaseDate> getAllSafetyEquipmentPurchaseDate(SafetyEquipmentPurchaseDate safetyEquipmentPurchaseDate) {
        if(safetyEquipmentPurchaseDate.getExaminer()!=null)
            return jdbcTemplate.query("select " +
                            "PurchaseDateId," +
                            "ItemName," +
                            "quantity," +
                            "examiner," +
                            "examineDate " +
                            "from safety_equipment_purchase_date where examiner='"+safetyEquipmentPurchaseDate.getExaminer()+"'",new Object[]{},
                new BeanPropertyRowMapper<>(SafetyEquipmentPurchaseDate.class));
        else
            return jdbcTemplate.query("select " +
                            "PurchaseDateId," +
                            "ItemName," +
                            "quantity," +
                            "examiner," +
                            "examineDate " +
                "from safety_equipment_purchase_date",new Object[]{},
                new BeanPropertyRowMapper<>(SafetyEquipmentPurchaseDate.class));
    }

    @Override
    public SafetyEquipmentPurchaseDate getSafetyEquipmentPurchaseDate(SafetyEquipmentPurchaseDate safetyEquipmentPurchaseDate) {
        List<SafetyEquipmentPurchaseDate> result=jdbcTemplate.query("select " +
                        "PurchaseDateId," +
                        "ItemName," +
                        "quantity," +
                        "examiner," +
                        "examineDate " +
                        "from safety_equipment_purchase_date where examiner='"+safetyEquipmentPurchaseDate.getExaminer()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(SafetyEquipmentPurchaseDate.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }

    @Override
    public SafetyEquipmentPurchaseDate getSafetyEquipmentPurchaseDateById(SafetyEquipmentPurchaseDate safetyEquipmentPurchaseDate) {
        List<SafetyEquipmentPurchaseDate> result=jdbcTemplate.query("select " +
                        "PurchaseDateId," +
                        "ItemName," +
                        "quantity," +
                        "examiner," +
                        "examineDate " +
                        "from safety_equipment_purchase_date where purchaseDateId='"+safetyEquipmentPurchaseDate.getPurchaseDateId()+"'",
                        new Object[]{},
                        new BeanPropertyRowMapper<>(SafetyEquipmentPurchaseDate.class));
        if (result.size()>=1)
            return result.get(0);
        else return null;
    }
}
